package org.rambadger.command.axon;

import org.axonframework.eventhandling.AbstractClusterSelector;
import org.axonframework.eventhandling.Cluster;
import org.axonframework.eventhandling.EventListener;

/**
 * Cluster selector that matches objects that are a subclass of a given class.
 */
public class SuperclassClusterSelector extends AbstractClusterSelector {
    private final Class<?> klass;
    private final Cluster cluster;

    /**
     * Creates a new {@link org.axonframework.eventhandling.ClusterSelector cluster selector} that pairs object to the given {@link Cluster cluster} if they belong to a class that is a subclass of the given class.
     *
     * @param klass the klass
     * @param cluster the cluster
     */
    public SuperclassClusterSelector(Class<?> klass, Cluster cluster) {
        this.klass = klass;
        this.cluster = cluster;
    }

    @Override
    protected Cluster doSelectCluster(EventListener eventListener, Class<?> listenerType) {
        return klass.isAssignableFrom(listenerType) ? cluster : null;
    }
}
